﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Data;
using Yipusoft.SecurityEvaluate;

public partial class Police_SecurityEvaluate_CarAnalyseLochus : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 2009; i < 2020; i++)
            {
                dptYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            for (int i = 1; i <= 12; i++)
            {
                dptMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            dptYear.SelectedValue = Request["year"];
            dptMonth.SelectedValue = Request["month"];
            Bind();
        }

        

    }
    //周五天数
    private int GetFridayCount(int year, int month)
    {
        int Count = 0;
        DateTime dt = new DateTime(year, month, 1);
        int LastDay = new DateTime(year, month + 1, 1).AddDays(-1).Day;
        for (int i = 1; i <= LastDay; i++)
        {
            if (Convert.ToInt32(new DateTime(year, month, i).DayOfWeek) == 5)
            {
                Count += 1;
            }
        }
        return Count;

    }
    private double GetCarCountByDate(DateTime Stime, DateTime EndTime, string DepartmentID, double avg)
    {
        double Score = 0;
        ParkLogDataContext db = new ParkLogDataContext();
        List<CarOrder> list = (from obj in db.CarOrder where obj.Date >= Stime && obj.Date <= EndTime && obj.DepartmentID == DepartmentID select obj).ToList();
        if (list == null || list.Count <= 0)
            return 0;
        foreach (CarOrder c in list)
        {
            List<CarInspect> listinspect = (from o in db.CarInspect where o.CID == c.ID select o).ToList();
            double temp = avg / listinspect.Count;
            Score += (temp * listinspect.Where(o => o.Capability == 1).Count());

        }
        return Score;
    }
    private int GetCarCount(string DepartmentID)
    {
        FireBaseDataContext db = new FireBaseDataContext();
        return (from obj in db.Vehicles where obj.DepartmentID.Equals(DepartmentID) select obj).Count();
    }
    private double GetDepartmentScore(int year, int month, string DepartmentID)
    {
        //录入车辆数
        int carCount = GetCarCount(DepartmentID);
        //周天数
        int fridayCount = GetFridayCount(int.Parse(dptYear.SelectedValue), int.Parse(dptMonth.SelectedValue));
        if (carCount == 0)
            return 0;
        int avg = 100 / carCount;
        double Score = fridayCount * 100;
        double sum = 0;

        DateTime dt = new DateTime(year, month, 1);
        int LastDay = new DateTime(year, month + 1, 1).AddDays(-1).Day;
        for (int i = 1; i <= LastDay; i++)
        {
            if (Convert.ToInt32(new DateTime(year, month, i).DayOfWeek) == 5)
            {
                DateTime s = new DateTime(year, month, i).AddDays(-4);
                DateTime e = s.AddDays(6);
                double temp = (GetCarCountByDate(s, e, DepartmentID, avg));
                sum += temp;

            }
        }
        if (Score == 0 || sum == 0)
            return 0;

        return sum / Score;

    }

    private void Bind()
    {
    
        FireMisDataContext db = new FireMisDataContext();
        List<Account_Department> list = (from obj in db.Account_Department where obj.DepartmentID.Substring(0, 4) == Request["DepartmentID"] && obj.DepartmentID.Substring(4, 1) == "3" select obj).ToList();
        DataTable dtb = new DataTable();
        DataColumn c1 = new DataColumn("DepartmentID", typeof(string));
        DataColumn c2 = new DataColumn("FullName", typeof(string));
        DataColumn c3 = new DataColumn("Score", typeof(double));
        c3.DefaultValue = 0;

        dtb.Columns.Add(c1);
        dtb.Columns.Add(c2);
        dtb.Columns.Add(c3);
        foreach (Account_Department a in list)
        {
            DataRow drw = dtb.NewRow();
            drw[0] = a.DepartmentID;
            drw[1] = a.ShortName;
            drw[2] = decimal.Round(decimal.Parse(GetDepartmentScore(int.Parse(dptYear.SelectedValue), int.Parse(dptMonth.SelectedValue), a.DepartmentID).ToString()), 2);
            dtb.Rows.Add(drw);

        }
        Chart1.Series["车辆状况"].Points.DataBindXY(dtb.DefaultView, "FullName", dtb.DefaultView, "Score");
        Chart1.Series["车辆状况"].LabelFormat = "p0";
        

    }
    protected void lbtnQuery_Click(object sender, EventArgs e)
    {
        Bind();
        Chart1.Titles[0].Text = Yipusoft.Account.BLL.Department.GetDepartment(Request["DepartmentID"] + "000").ShortName + "各中队执勤车辆性能统计分析图";
    }
}
